package com.roc.web.action.estrn;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Estrnlog;
import com.roc.pojo.Operators;
import com.roc.pojo.TransReport;
import com.roc.service.IPagenitionService;
import com.roc.service.ServiceException;
import com.roc.util.Constance;
import com.roc.util.constant.Constant;
@Component
@Scope("prototype")
@Namespace("/bigEstrnHisQuery")
public class BigEstrnHisAction extends PaginationBaseAction{

    private Logger logger=LogManager.getLogger(BigEstrnHisAction.class);
    private BigEstrnHisSearchCondition searchCondition;
    private List<Estrnlog> estrnList=new ArrayList<Estrnlog>();
    private int flag=0;
	
	@Autowired
	IPagenitionService pagenitionService;
	
	
	@Action(value = "init", results = { @Result(name = "success", location = "/pages/estrn/queryBigEstrn.jsp")
    })
	public String init(){
		return SUCCESS;
	}
	
	@Action(value = "queryBigEstrnHis", results = { @Result(name = "success", location = "/pages/estrn/queryBigEstrn.jsp")
    })
	public String queryBigEstrnHis(){
		Operators loginOperators = (Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		
		try {
			
			init();
			if(Constance.Null2EmptyTrim(searchCondition.getMinDate()).equals("")||Constance.Null2EmptyTrim(searchCondition.getMaxDate()).equals("")){
				getRequest().setAttribute("msg", "请选择时间");
				
				return SUCCESS;
				
			}
			int rows=pagenitionService.countSQL(searchCondition);
			if(rows>0){
				searchRsList=pagenitionService.findAllSQL(searchCondition, calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
				
			}
			
			dealWidth();
			flag=1;
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(SEARCH_RS_LIST, estrnList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(searchCondition);
			
		}catch(ServiceException e){
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
		}
		
		return SUCCESS;
	}
	
	private void dealWidth(){
		if(searchRsList.size()>0 && searchRsList!=null){
			
			Iterator iterator=searchRsList.iterator();
			while (iterator.hasNext()) {
				Object[] obj= (Object[])iterator.next();
				Estrnlog temp=new Estrnlog();
				temp.setAccount(Constance.Null2EmptyTrim(obj[0]));
				temp.setAmount(Constance.Null2EmptyTrim(obj[1]));
				temp.setMerNo(Constance.Null2EmptyTrim(obj[2]));
				temp.setEtsterMid(Constance.Null2EmptyTrim(obj[3]));
				temp.setEtsDate(Constance.Null2EmptyTrim(obj[4]));
				temp.setMerName(Constance.Null2EmptyTrim(obj[5]));
				temp.setMcc(Constance.Null2EmptyTrim(obj[6]));
				temp.setEngName(Constance.Null2EmptyTrim(obj[7]));
				temp.setEngBrName(Constance.Null2EmptyTrim(obj[8]));
				
				estrnList.add(temp);
			}
		}
		//return null;
	}
	
	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		// TODO Auto-generated method stub
		return searchCondition;
	}

	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		// TODO Auto-generated method stub
		this.setSearchConditionToSubClass(searchCondition);
	}

	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		// TODO Auto-generated method stub
		getSession().setAttribute(SEARCH_SESSION_BIGESTRNHIS_QUERY, searchCondition);
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		// TODO Auto-generated method stub
		return (BaseSearchCondition) getSession().getAttribute(SEARCH_SESSION_BIGESTRNHIS_QUERY);
	}

	@Override
	protected String getInitPageSize() {
		// TODO Auto-generated method stub
		return  Constance.PAGE_MAX_SIZE+"";
	}

	public BigEstrnHisSearchCondition getSearchCondition() {
		return searchCondition;
	}

	public void setSearchCondition(BigEstrnHisSearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}

	public List<Estrnlog> getEstrnList() {
		return estrnList;
	}

	public void setEstrnList(List<Estrnlog> estrnList) {
		this.estrnList = estrnList;
	}

	public int getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}
	

}
